<?php namespace simpleFlow\control;

use simpleFlow\Object as SFObject;
use simpleFlow\view\View as SFView;
use simpleFlow\control\Control as SFControl;
use simpleFlow\Session as SFSession;
use simpleFlow\request\RequestWeb as SFRequestWeb;
use simpleFlow\Config as SFConfig;

/**
 * ControlManager thats decode the request
 * @package simpleFlow
 * @subpackage control
 * @author Guilherme Fontenele
 */
class ControlManager extends SFObject
{
	/**
	 * @var SFControl Controller 
	 */
	public static $objControl;
	
	/**
	 * Start application
	 */
	public static function start()
	{
		$arrConfigFileContent = SFConfig::readFile(SF_APP_DIR_CONFIG.'app.ini');
		
		$arrParams = SFRequestWeb::getRequest()->getParams();
		
		$arrCalls = explode('/', $_SERVER['REQUEST_URI']);
		array_shift($arrCalls);
		
		//TODO Verify if the app is 1 to 6 under directorys, if possible use app.ini to define routes
		if(!SF_VIRTUAL_HOST)
		{
			array_shift($arrCalls);
			array_shift($arrCalls);
		}
		
		switch(true)
		{
			// empty path
			case !isset($arrCalls[0]) || !$arrCalls[0]:
				$arrParams['_module_']	= $arrConfigFileContent['routes']['default']['module'];
				$arrParams['_app_']		= $arrConfigFileContent['routes']['default']['app'];
				$arrParams['_action_']	= $arrConfigFileContent['routes']['default']['action'];
				break;
			// if unlocked
			case key_exists($arrCalls[0],$arrConfigFileContent['unlockeds']) && 
				 key_exists("{$arrCalls[1]}/{$arrCalls[2]}",$arrConfigFileContent['unlockeds'][$arrCalls[0]]):
			case \SF_MANAGER_UNLOCKED:
				$arrParams['_module_']	= $arrCalls[0];
				$arrParams['_app_']		= $arrCalls[1];
				$arrParams['_action_']	= $arrCalls[2];
				break;
			// if not logged
			case !SFSession::getParam('SF_USER_LOGGED'):
				$arrParams['_module_']	= $arrConfigFileContent['routes']['default']['module'];
				$arrParams['_app_']		= $arrConfigFileContent['routes']['default']['app'];
				$arrParams['_action_']	= $arrConfigFileContent['routes']['default']['action'];
				SFSession::addParam('sf_error_message', 'You are not logged!');
				break;
			default:
				$arrParams['_module_']	= $arrCalls[0];
				$arrParams['_app_']		= $arrCalls[1];
				$arrParams['_action_']	= $arrCalls[2];
				break;
		}
		
		if(!SFSession::hasParam('sf_config') && is_array($arrConfigFileContent))
		{
			SFSession::addParam('sf_config', $arrConfigFileContent);
		}
		
		if(file_exists(SF_APP_DIR_ROOT.'module'.DIRECTORY_SEPARATOR.$arrParams['_module_'].DIRECTORY_SEPARATOR.'control'.DIRECTORY_SEPARATOR.$arrParams['_app_'].DIRECTORY_SEPARATOR.$arrConfigFileContent['routes']['class'].'.php'))
		{
			$objRequest = new SFRequestWeb($arrParams);
			
			require_once(SF_APP_DIR_ROOT.'module'.DIRECTORY_SEPARATOR.$arrParams['_module_'].DIRECTORY_SEPARATOR
					.'control'.DIRECTORY_SEPARATOR.$arrParams['_app_'].DIRECTORY_SEPARATOR
					.$arrConfigFileContent['routes']['class'].'.php');
			
			$strClass = "\simpleFlow\app\module\\{$arrParams['_module_']}\control\\{$arrParams['_app_']}\\{$arrConfigFileContent['routes']['class']}";
			$objCtrl = new $strClass(new SFView(SF_APP_DIR_ROOT.'module'.DIRECTORY_SEPARATOR.$arrParams['_module_']
					.DIRECTORY_SEPARATOR.'view'.DIRECTORY_SEPARATOR.$arrParams['_app_'].DIRECTORY_SEPARATOR));
			
			if(!$objCtrl->__checkPermission() && 
					!(key_exists($arrCalls[0],$arrConfigFileContent['unlockeds']) && 
					key_exists("{$arrCalls[1]}/{$arrCalls[2]}",$arrConfigFileContent['unlockeds'][$arrCalls[0]]) ))
			{
				SFSession::addParam('sf_error_message', 'You dont have permission!');
				$objCtrl->redirect($arrConfigFileContent['routes']['default']['module'],
						$arrConfigFileContent['routes']['default']['app'],
						$arrConfigFileContent['routes']['default']['action'],$arrParams);
			}else{
				$objCtrl->objView->objRequest = $objRequest;
				$objCtrl->$arrParams['_action_']($objRequest);
				$objCtrl->objView->objTheme->show();
			}
		}
	}
}

?>